import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import(/*webpackChunkName: 'login'*/'@/views/login/Login')
  },
  {
    path: '/',
    component: () => import(/* webpackChunkName: 'homepage' */'@/views/homepage/Homepage'),
    children: [
      {
        path: '/employee',
        meta: {
          mytitle: '员工管理'
        },
        component: () => import(/*webpackChunkName: 'employee'*/'@/views/employee/Employee')
      },
      {
        path: '/department',
        meta: {
          mytitle: '部门管理'
        },
        component: () => import(/*webpackChunkName: 'department'*/'@/views/department/Department')
      },
      {
        path: '/permission',
        meta: {
          mytitle: '权限管理'
        },
        component: () => import(/*webpackChunkName: 'permission'*/'@/views/permission/Permission')
      },
      {
        path: '/employee/add',
        meta: {
          mytitle: '员工新增',
          index: '/employee'
        },
        component: () => import(/*webpackChunkName: 'addemployee'*/'@/views/employee/AddEmployee')
      },
      {
        path: '/employee/edit/:eid',
        meta: {
          mytitle: '员工编辑',
          index: '/employee'
        },
        component: () => import(/*webpackChunkName: 'addemployee'*/'@/views/employee/AddEmployee')
      },
      {
        path: '/customerreport',
        meta: {
          mytitle: '潜在客户报表',
        },
        component: () => import(/*webpackChunkName: 'addemployee'*/'@/views/charts/Charts')
      },
    ]
  },
  {
    path: '*',
    redirect: '/employee'
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('token') && to.path != '/login') {
    return next('/login')
  } else if (localStorage.getItem('token') && to.path == '/login') {
    return next(from)
  }
  next();
})

export default router
